/**
 * @author 鍚存倍
 * @since 2022/10/13
 * 17:03
 */
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.bo.BrandCreateBo;
import com.windgo.bean.MarketBrand;
import com.windgo.bean.MarketBrandExample;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.MarketBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData<MarketBrand> list(BasePageInfo basePageInfo, String name, Integer id) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketBrand> list = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(list);
        return CommonData.data(marketBrandPageInfo);


    }

    @Override
    public int delete(Integer id) {
        int i = marketBrandMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public MarketBrand insert(MarketBrand marketBrand) {
        //  MarketBrand brand = brandCreateBo.getBrand();
        Date date = new Date();
        marketBrand.setAddTime(date);
        marketBrand.setUpdateTime(date);
        /*MarketBrandExample marketBrandExample1 = new MarketBrandExample();
        MarketBrandExample.Criteria criteria1 = marketBrandExample1.createCriteria();
        criteria1.andDescEqualTo(marketBrand.getDesc());
        criteria1.andFloorPriceEqualTo(marketBrand.getFloorPrice());
        criteria1.andPicUrlEqualTo(marketBrand.getPicUrl());
        criteria1.andNameEqualTo(marketBrand.getName());

         */
        int i = marketBrandMapper.insertSelective(marketBrand);
        Integer id = marketBrand.getId();
        if (i != 1) {
            return null;
        }
        marketBrand.setId(id);
        return marketBrand;
       /* int i = marketBrandMapper.insertSelective(brand);
        if(i!=1){
            return null;
        }
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
         criteria.andAddTimeEqualTo(date);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        for (MarketBrand marketBrand : marketBrands) {
            Integer id = marketBrand.getId();
            brand.setId(id);
        }
        return brand;

        */
    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        Date date = new Date();
        marketBrand.setUpdateTime(date);
        int i = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        if (i != 1) {
            return null;
        }
        return marketBrand;
    }

    /**
     * 给wx小程序分页返回品牌信息
     *
     * @param info
     * @return com.windgo.bean.common.CommonData<com.windgo.bean.MarketBrand>
     * @author mabai
     * @since 2022/10/14 15:09
     */
    @Override
    public CommonData<MarketBrand> wxList(BasePageInfo info) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketBrandExample example = new MarketBrandExample();
        List<MarketBrand> list = marketBrandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 返回品牌详情信息
     *
     * @param id
     * @return com.windgo.bean.common.CommonData<com.windgo.bean.MarketBrand>
     * @author mabai
     * @since 2022/10/14 15:30
     */
    @Override
    public MarketBrand brandDetail(Integer id) {
        return marketBrandMapper.selectByPrimaryKey(id);
    }
}

